$(function(){
	myselfChoose(1);

	$("#deleteThisCourse").click(function () {

       $.post("/deleteCourseByCid",{"cid":$("#courseId").val()},function (data) {
		   if(data==1){
               $("#info").css("display","block");
               $("#msg").html("<p style='font-size:50px;padding-top:30px;color:green;'>成功</p>");
               window.setTimeout(function(){
                   $("#info").css("display","none");
               },1500);
               myselfChoose(1);//删除成功则重新刷新页面的信息
		   }else{
               $("#info").css("display","block");
               $("#msg").html("<p style='font-size: 20px;color: red;'>该课程正在进行</p>");
               window.setTimeout(function(){
                   $("#info").css("display","none");
               },1500);
		   }
       });
    });


	$("#updateCourse").click(function(){
		var updateInfo = {"cid":$("#courseId").val(),
		"cname":$("#courseName").val(),
		"teacher":$("#courseTeacher").val(),
		"week":$("#courseWeek").val(),
		"schooltime":$("#courseSchooltime").val(),
		"mold":$("#courseMold").val(),
		"venue":$("#courseVenue").val(),
		"price":$("#coursePrice").val()};
		var cname = $("#courseName").val().replace(/(^\s*)|(\s*$)/g, '');
		var teacher = $("#courseTeacher").val().replace(/(^\s*)|(\s*$)/g, '');
		var price = $("#coursePrice").val();
		if(isNaN(price)){
			return false;
		}else if(cname == '' || cname == undefined || cname == null){
			alert(1);
			return false;
		}else if(teacher == '' || teacher == undefined || teacher == null){
			alert(1);
			return false;
		}
		console.log(updateInfo);
		$.post("/updateCourse",updateInfo,function (data) {
			if (data==1){
                $("#info").css("display","block");
                $("#msg").html("<p style='font-size:50px;padding-top:30px;color:green;'>成功</p>");
                window.setTimeout(function(){
                    $("#info").css("display","none");
                },1500);
			} else{
                $("#info").css("display","block");
                $("#msg").html("<p style='font-size: 20px;color: red;'>与已存在课程存在冲突</p>");
                window.setTimeout(function(){
                    $("#info").css("display","none");
                },1500);
			}
        });
        myselfChoose(1);//更新完成之后,在重新获取当前页面的信息
	});
	//课程添加相关js
	$("#addCourse").click(function(){
		var course = {
		"cname":$("#addcourseName").val(),
		"teacher":$("#addcourseTeacher").val(),
		"sample":$("#fileName").val(),
		"week":$("#addcourseWeek").val(),
		"schooltime":$("#addcourseSchooltime").val(),
		"mold":$("#addcourseMold").val(),
		"venue":$("#addcourseVenue").val(),
		"price":$("#addcoursePrice").val()};
		var cname = $("#addcourseName").val().replace(/(^\s*)|(\s*$)/g, '');
		var teacher = $("#addcourseTeacher").val().replace(/(^\s*)|(\s*$)/g, '');
		var price = $("#addcoursePrice").val();
		if(isNaN(price)||price == '' || price == undefined || price == null){
			 $("#info").css("display","block");
    	 $("#msg").html("<p style='font-size:30px;color:red;padding-top:30px;'>价格不是数字</p>");
    	 window.setTimeout(function(){
    	 	$("#info").css("display","none");
    	 },1500);
			return false;
		}else if(cname == '' || cname == undefined || cname == null){
			 $("#info").css("display","block");
    	 $("#msg").html("<p style='font-size:30px;color:red;padding-top:30px;'>名称不能为空</p>");
    	 window.setTimeout(function(){
    	 	$("#info").css("display","none");
    	 },1500);
			return false;
		}
		console.log(course);
        $.post("/addCourse",course,function (data) {
            if(data==1){
                $("#info").css("display","block");
                $("#msg").html("<p>&nbsp;</p>" +
                    "<p style='font-size: 40px;'>成功</p>");
                window.setTimeout(function(){
                    $("#info").css("display","none");
                },1500);
            }else{
                $("#info").css("display","block");
                $("#msg").html("<p>失败</p>" +
                    "<p style='font-size: 20px;color: red;'>与已存在课程存在冲突</p>");
                window.setTimeout(function(){
                    $("#info").css("display","none");
                },2500);
            }
        });
	});
//上传图片
	$("#pic").click(function () {
        $("#upload").click(); //隐藏了input:file样式后，点击头像就可以本地上传
        $("#upload").on("change",function(){
            var formData = new FormData();
            formData.append('file', $('#upload')[0].files[0]);
            formData.append("selectId", '0');
            console.log(formData);
            $.ajax({
                url: "/courseUpload",
                type: "POST",
                data: formData,
                contentType: false,
                processData: false,
                success: function (data) {
                    $("#fileName").val(data);
                },
                error: function () {
                    alert("上传失败！");

                }
         });							
		var objUrl = getObjectURL(this.files[0]) ; //获取图片的路径，该路径不是图片在本地的路径
		if (objUrl) {
				$("#pic").attr("src", objUrl) ; //将图片路径存入src中，显示出图片
		}
		});
		});
});
	//取消图片
    function stopUpload(){ 	
    	$("#pic").attr("src","images/feature-thumb1.png");
    	$("#fileName").val("feature-thumb1.png");
    }
	    //建立一個可存取到該file的url
	function getObjectURL(file) {	
		var url = null ;
		if (window.createObjectURL!=undefined) { // basic
			url = window.createObjectURL(file) ;
		} else if (window.URL!=undefined) { // mozilla(firefox)
			url = window.URL.createObjectURL(file) ;
		} else if (window.webkitURL!=undefined) { // webkit or chrome
			url = window.webkitURL.createObjectURL(file) ;
		}
		return url ;
	}				
    

var data = [{"cid":1,
	"cname":"力量举硬拉",
	"sample":"feature-thumb1.png",
	"teacher":"王可可",
	"mold":"增肌课",
	"week":2,
	"schooltime":"19:00",
	"venue":"无氧区",
	"price":200,
	"searchCount":2333,
	"orderCount":10},{"cid":2,
	"cname":"力量举硬拉",
	"sample":"feature-thumb2.png",
	"teacher":"王可可",
	"mold":"增肌课",
	"week":2,
	"schooltime":"19:00",
	"venue":"无氧区",
	"price":200,
	"searchCount":232,
	"orderCount":220},{"cid":3,
	"cname":"力量举硬拉",
	"sample":"feature-thumb1.png",
	"teacher":"王可可",
	"mold":"增肌课",
	"week":2,
	"schooltime":"19:00",
	"venue":"无氧区",
	"price":200,
	"searchCount":230,
	"orderCount":210}];

function myselfChoose(i){
	if(i==1){
		//改变导航栏样式
		$("#oneLi").addClass("active");
		$("#twoLi").removeClass("active");       			
		//改变所展示的课程信息的显示情况
		$("#oneContainer").css("display","block");
		$("#twoContainer").css("display","none");
        $.post("/getCourseByCName",{"cname":$("#nowCName").val()},function (data){
		var String = "";
		for(var i=0;i<data.length;i++){
			String = String +
			"<div class='col-sm-4 col-xs-12'>"+
               "<a href='javascript:;' onclick='getMyCourseInfo("+data[i].cid+")' data-toggle='modal' data-target='#myModal'>"+
                   "<div class='feature-box'>"+
                        "<figure>"+
                            "<img src='images/"+data[i].sample+"'>"+
                            "<div class='time-box'>"+
                                "<span class='date'><span>点击量："+data[i].searchCount +"</span></span>"+
                                "<span class='time'><span>预约量："+data[i].orderCount +"</span></span>"+
                           "</div>"+
                        "</figure>"+
                    "</div>"+
                 "</a>"+
			 "</div>";
		}
		$("#myCourseDiv").html(String);
        });
	}
	if(i==2){
		$("#oneLi").removeClass("active");
		$("#twoLi").addClass("active");       		
		$("#oneContainer").css("display","none");
		$("#twoContainer").css("display","block");
	}
}    

//获取课程信息并赋值前端
function getMyCourseInfo(cid){
    $.post("/getCourseByCid",{"cid":cid},function (data){
        $("#courseId").val(data.cid);
        $("#courseName").val(data.cname);
        $("#courseMold").val(data.mold);
        $("#courseSample").attr("src","images/"+data.sample);
        $("#courseWeek").val(data.week);
        $("#courseSchooltime").val(data.schooltime);
        $("#courseTeacher").val(data.teacher);
        $("#courseVenue").val(data.venue);
        $("#coursePrice").val(data.price);
    });
}
